var checkedlist = {
    page: 1,
    init: function() {
        var that = this;
        that.dateInit();

        
        that.doData();

        $("#ched-query").click(function() {
            // that.doSearch();
            // 重置分页
            that.page = 0;
            that.doData();
        });
    },

    dateInit: function() {
        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            format: 'yyyy-mm-dd hh:ii'
        });
    },
    format: function(nS) {
        //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
        if (!nS) return null;
        var date = new Date(nS * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var d = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        return year + '-' + month + '-' + d + ' ' + hours + ':' + minutes;
    },
    /**
     * 页面数据渲染
     * @return {[type]} [description]
     */
    doData: function() {
        var that = this;
        // 获取搜素参数
        var bid_no = $.trim($("#cke-bid_no").val()),
            ordernumber = $.trim($("#cke-orderNumber").val()),
            state_bid_time1 = $.trim($("#State_Bid_Time1").val()) == '' ? '' : new Date($.trim($("#State_Bid_Time1").val())).getTime(),
            state_bid_time2 = $.trim($("#State_Bid_Time2").val()) == '' ? '' : new Date($.trim($("#State_Bid_Time2").val())).getTime(),
            end_bid_time1 = $.trim($("#End_Bid_Time1").val()) == '' ? '' : new Date($.trim($("#End_Bid_Time1").val())).getTime(),
            end_bid_time2 = $.trim($("#End_Bid_Time2").val()) == '' ? '' : new Date($.trim($("#End_Bid_Time2").val())).getTime();

        // 请求数据并渲染列表
        $.ajax({
            url: COMMONURL + "api/ReportBase/GetPageList?token=" + $.cookie('token'),
            type: "post",
            data: {
                bid_no: bid_no,
                ordernumber: ordernumber,
                consignee_time1: state_bid_time1,
                consignee_time2: state_bid_time2,
                pickup_time1: end_bid_time1,
                pickup_time2: end_bid_time2,
                "page": that.page,
                "pagesize": 20,
                // 表示未结账
                state: 100
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod.Items.length > 0) {

                var compiled = _.template($("#CheckedlistTpl").html());
                var odata = redata.mod.Items;
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i]._pickup_time = that.format(odata[i]._pickup_time);
                    odata[i]._start_bid_time = that.format(odata[i]._start_bid_time);
                    odata[i]._report_time = that.format(odata[i]._report_time);
                }
                $("#CheckedlisttabCon").html(compiled(redata.mod));
                $("#ched-page").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage:  that.page,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {

                        that.page = num;
                        if (type == 'change') {
                            that.doData();
                        }
                    }
                });

            } else {

                var compiled = _.template($("#CheckedlistTpl").html());
                var odata = redata.mod.Items;
                $("#CheckedlisttabCon").html(compiled(redata.mod));
                $("#ched-page").hide();
            }
        });
    }
}

checkedlist.init();
